﻿#include <QMutexLocker>
#include <QDir>
#include <QApplication>
#include <QSettings>

#include <languages_manager/qls_languages_manager.h>
#include <settings/qls_settings.h>

/* static */
QMap<QString, QString> QLsLanguagesManager::m_languages_map = {
    { "zh_CN", "中文 (简体)" },
    { "en", "English" },
    { "ar", "العربية" },
    { "bg", "Български" },
    { "ca", "Català" },
    { "cs", "Čeština" },
    { "da", "Dansk" },
    { "de", "deutsch" },
    { "es", "Español" },
    { "fi", "Suomi" },
    { "fr", "Français" },
    { "gd", "gd" },
    { "he", "he" },
    { "hu", "Magyar" },
    { "it", "Testo italiano" },
    { "ja", "にっぽん" },
    { "ko", "한국어" },
    { "lv", "Latviešu (Leto)" },
    { "pl", "Polski" },
    { "ru", "Русский язык (Россия)" },
    { "sk", "Slovenčina" },
    { "uk", "Українська" },
    { "zh_TW", "中文 (繁體)" }
};

QMutex QLsLanguagesManager::m_instanceMutex;
QLsLanguagesManager *QLsLanguagesManager::m_instance = nullptr;

QLsLanguagesManager::QLsLanguagesManager(QObject *parent) : QObject(parent)
{
    m_langPacksList.clear();
    m_curLangPack.clear();
    m_curTranslator = nullptr;

    /* 初始化可用语言包列表 */
    RefreshLocalLangPacks();

    /* 初始化当前语言包 */
    QSettings settings(QLS_FILEPATH_SETTINGS, QSettings::IniFormat);
    QString curLangPack = settings.value(QLS_KEY_LANGUAGE, "zh_CN").toString();
    LoadLangPack(curLangPack);
}

/* 唯一实例 */
QLsLanguagesManager &QLsLanguagesManager::GetInstance()
{
    if (!m_instance) {
        QMutexLocker locker(&m_instanceMutex);
        if (!m_instance)
            m_instance = new QLsLanguagesManager;
    }
    return *m_instance;
}

/* 销毁实例 */
void QLsLanguagesManager::DestroyInstance()
{
    if (m_instance) {
        QMutexLocker locker(&m_instanceMutex);
        if (m_instance) {
            delete m_instance;
            m_instance = nullptr;
        }
    }
}

/* 刷新本地语言包 */
void QLsLanguagesManager::RefreshLocalLangPacks()
{
    // 清空上次记录
    m_langPacksList.clear();

    // 打开本地语言包文件夹
    QDir langPacksDir(QString("%1/%2").arg(QLsSettings::getInstallPath(),
                                           QLS_PATH_LANGUAGE_PACKS));
    if (!langPacksDir.exists()) {
        return;
    }

    for (QString filename : langPacksDir.entryList()) {
        /* 获取 qlpmsp_*.qm 语言包文件 */
        if (!filename.startsWith("qlpmsp_") || !filename.endsWith(".qm")) {
            continue;
        }

        /* 加入列表 */
        m_langPacksList.push_back(
            filename.mid(strlen("qlpmsp_")).chopped(strlen(".qm")));
    }
}

/* 加载语言包 */
bool QLsLanguagesManager::LoadLangPack(const QString &langPackName)
{
    /* 判断当前语言包是否存在 或 有无变化 */
    if (!m_langPacksList.contains(langPackName) ||
        m_curLangPack == langPackName) {
        return false;
    }

    /* 判断语言包文件夹和判断当前语言包文件是否存在 */
    QString langPackFilename = QString("qlpmsp_%1.qm").arg(langPackName);
    QDir langPacksDir(QString("%1/%2").arg(QLsSettings::getInstallPath(),
                                           QLS_PATH_LANGUAGE_PACKS));
    if (!langPacksDir.entryList().contains(langPackFilename)) {
        return false;
    }

    /* 加载语言包：先清空上次的 */
    if (m_curTranslator) {
        qApp->removeTranslator(m_curTranslator);
        delete m_curTranslator;
        m_curTranslator = nullptr;
    }
    QTranslator *translator = new QTranslator;
    if (!translator->load(langPackFilename, langPacksDir.absolutePath())) {
        delete translator;
        return false;
    }
    qApp->installTranslator(translator);
    m_curLangPack = langPackName;
    m_curTranslator = translator;

    /* 更新配置文件 */
    QSettings settings(QLS_FILEPATH_SETTINGS, QSettings::IniFormat);
    settings.setValue(QLS_KEY_LANGUAGE, langPackName);
    return true;
}

/* 获取可用语言包列表 */
QStringList QLsLanguagesManager::GetLangPacks() const
{
    return m_langPacksList;
}

/* 获取当前语言包 */
QString QLsLanguagesManager::GetCurrentLangPack() const
{
    return m_curLangPack;
}

QMap<QString, QString> QLsLanguagesManager::getLanguagesMap()
{
    return m_languages_map;
}
